package com.example.enterprisemanager.mvvm.fragment.manager;

import com.example.enterprisemanager.R;
import com.example.enterprisemanager.base.BaseModel;
import com.example.enterprisemanager.mvvm.ben.User;
import com.example.enterprisemanager.mvvm.fragment.manager.ben.PurchaseGoods;
import com.example.enterprisemanager.mvvm.fragment.manager.ben.WaitPurchase;
import com.orhanobut.logger.Logger;

import java.io.File;
import java.nio.channels.AcceptPendingException;
import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobBatch;
import cn.bmob.v3.BmobObject;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BatchResult;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

public class WaitPurchaseModel extends BaseModel {
    private Disposable mInsertDisposable;
    private Disposable mGetShoppingDisposable;
    private Disposable mPurchaseDisposable;
    private Disposable mAfterPurchaseDisposable;

    private String[] storageName = {"仓库一", "仓库二", "仓库三"};

    public void insertGoods(Consumer<List<BatchResult>> success, Consumer<Exception> fail) {
        BmobFile bmobFile = new BmobFile(new File(String.valueOf(R.drawable.dayinji)));
        bmobFile.uploadblock(new UploadFileListener() {

            @Override
            public void done(BmobException e) {
                if(e==null){
                    //bmobFile.getFileUrl()--返回的上传文件的完整地址
                }else{

                }
            }

            @Override
            public void onProgress(Integer value) {
                // 返回的上传进度（百分比）
            }
        });


        List<BmobObject> data = new ArrayList<>();

        WaitPurchase pur1 = new WaitPurchase("打印机", "惠普", null, "固定资产", 3500, 500,0);
        WaitPurchase pur2 = new WaitPurchase("办公桌", "呆头", null, "固定资产", 900, 300,0);
        WaitPurchase pur3 = new WaitPurchase("签字笔", "晨光", null, "耗材", 350, 5000,0);
        WaitPurchase pur4 = new WaitPurchase("咖啡机", "九阳", null, "固定资产", 300, 100,0);

        data.add(pur1);
        data.add(pur2);
        data.add(pur3);
        data.add(pur4);

        mInsertDisposable = new BmobBatch().insertBatch(data).doBatch(new QueryListListener<BatchResult>() {
            @Override
            public void done(List<BatchResult> list, BmobException e) {
                try {
                    if (e == null) {
                        success.accept(list);
                    } else {
                        fail.accept(e);
                    }
                } catch (Exception e1) {

                }
            }
        });
    }

    public void getShopping(Consumer<List<WaitPurchase>> success, Consumer<Exception> fail) {
        BmobQuery<WaitPurchase> query = new BmobQuery<>();
        mGetShoppingDisposable = query.findObjects(new FindListener<WaitPurchase>() {
            @Override
            public void done(List<WaitPurchase> list, BmobException e) {
                try {
                    if (e == null) {
                        success.accept(list);
                    } else {
                        fail.accept(e);
                    }
                } catch (Exception e1) {

                }
            }
        });
    }

    public void purchaseGoods(WaitPurchase purchase, int num, Action success, Consumer<Exception> fail) {
        mPurchaseDisposable = purchase.update(purchase.getObjectId(), new UpdateListener() {
            @Override
            public void done(BmobException e) {
                try {
                    if (e == null) {

                        success.run();

                        PurchaseGoods goods = new PurchaseGoods(purchase.getName(), purchase.getCompany(), purchase.getAvatar()
                                , purchase.getPrice(), purchase.getType(), BmobUser.getCurrentUser(User.class).getUsername(), storageName[(int) (Math.random() * 3)], num, num,0);
                        afterSuccessPurchase(goods);

                    } else {
                        fail.accept(e);
                    }
                } catch (Exception e1) {

                }
            }
        });

    }

    public void afterSuccessPurchase(PurchaseGoods purchaseGoods) {
        mAfterPurchaseDisposable = purchaseGoods.save(new SaveListener<String>() {
            @Override
            public void done(String objectId, BmobException e) {
                try {
                    if (e == null) {
                        Logger.d("afterSuccessPurchase success");

                    } else {
                        Logger.d(e.getMessage());
                    }
                } catch (Exception e1) {

                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mInsertDisposable != null) {
            mInsertDisposable.dispose();
            mInsertDisposable = null;
        }
        if (mGetShoppingDisposable != null) {
            mGetShoppingDisposable.dispose();
            mGetShoppingDisposable = null;
        }
        if (mPurchaseDisposable != null) {
            mPurchaseDisposable.dispose();
            mPurchaseDisposable = null;
        }
        if (mAfterPurchaseDisposable != null) {
            mAfterPurchaseDisposable.dispose();
            mAfterPurchaseDisposable = null;
        }
    }
}
